﻿using System;
using System.Data.Objects;
using System.Linq;
using System.Web.Mvc;
using BLL;
using Model;


namespace Web.Areas.Manager.Controllers
{

    [UserAuthorize(CheckRole = false)]
    public partial class IndexController : Controller
    {
        Sys_Users_BLL _Sys_Users_BLL = new Sys_Users_BLL();
        Sys_User_Notification_BLL _Sys_User_Notification_BLL = new Sys_User_Notification_BLL();

        public virtual ActionResult Index()
        {

            //读取权限菜单

            Sys_Controllers_BLL _Sys_Controllers_BLL = new Sys_Controllers_BLL();

            var Menus = _Sys_Controllers_BLL.Get().Where(a => a.Enable && a.Sys_ActionsInControllersInRoles.Any(c => c.Sys_Role.Sys_UsersInRoles.Any(d => d.Sys_Users.Id.Equals(new Guid(User.Identity.Name))))).OrderBy(a => a.SystemCode);

            Sys_Parameter_BLL _Sys_Parameter_BLL = new Sys_Parameter_BLL();

            ViewData["Title"] = _Sys_Parameter_BLL.Get("系统主名称");

            ViewData["Title1"] = _Sys_Parameter_BLL.Get("系统副名称");

            ViewData["User"] = _Sys_Users_BLL.Get(User.Identity.Name);

            return View(Menus.ToList());
        }

        //桌面
        public virtual ActionResult Desktop(string url)
        {
            var UserId = new Guid(User.Identity.Name);

            //读取今日应上班时间
            TimeCardSet_BLL _TimeCardSet_BLL = new TimeCardSet_BLL();

            var timecardset = _TimeCardSet_BLL.Get().Where(a => a.StartDate < DateTime.Now && a.EndDate > DateTime.Now).OrderBy(a => EntityFunctions.DiffSeconds(a.StartDate, a.EndDate)).FirstOrDefault();

            if (timecardset != null)
            {
                ViewBag.TodayWorkTime = timecardset.WorkTime;
                ViewBag.TodayAfterWorkTime = timecardset.AfterWorkTime;
            }
            else
            {
                ViewBag.TodayWorkTime = "未设定";
                ViewBag.TodayAfterWorkTime = "未设定";
            }

            //记录用户第一次登录时间
            TimeCard_BLL _TimeCard_BLL = new TimeCard_BLL();

            if (!_TimeCard_BLL.Get().Any(a => a.UserId.Equals(UserId) && a.AddDate.Year == DateTime.Now.Year && a.AddDate.Month == DateTime.Now.Month && a.AddDate.Day == DateTime.Now.Day))
            {
                TimeCard _TimeCard = new TimeCard { UserId = UserId, Note = "" };
                _TimeCard_BLL.Save(null, ref _TimeCard);
            }

            //显示上班时间
            var timecard = _TimeCard_BLL.Get().FirstOrDefault(a => a.UserId.Equals(UserId) && a.AddDate.Year == DateTime.Now.Year && a.AddDate.Month == DateTime.Now.Month && a.AddDate.Day == DateTime.Now.Day);

            if (timecard != null)
            {
                ViewBag.WorkTime = timecard.WorkTime;

                ViewBag.Note = timecard.Note;
                //显示下班时间
                if (timecard.AfterWorkTime != timecard.WorkTime)
                    ViewBag.AfterWorkTime = timecard.AfterWorkTime;
            }

            //读取最新的通知公告

            Notice_BLL _Notice_BLL = new Notice_BLL();
            ViewBag.Notice = _Notice_BLL.Get().Take(5);

            //显示今日重点
            TodayFocu_BLL _TodayFocu_BLL = new TodayFocu_BLL();
            ViewBag.TodayFocu = _TodayFocu_BLL.Get().FirstOrDefault();

            return View();
        }

        //功能模块地图
        public virtual ActionResult Map()
        {
            var UserId = new Guid(User.Identity.Name);

            Sys_Controllers_BLL _Sys_Controllers_BLL = new Sys_Controllers_BLL();

            var Menus = _Sys_Controllers_BLL.Get().Where(a => a.Enable && a.Sys_ActionsInControllersInRoles.Any(c => c.Sys_Role.Sys_UsersInRoles.Any(d => d.Sys_Users.Id.Equals(UserId)))).OrderBy(a => a.SystemCode);

            return View(Menus.ToList());
        }

        //点击下班
        public string AfterWork()
        {
            TimeCard_BLL _TimeCard_BLL = new TimeCard_BLL();

            var UserId = new Guid(User.Identity.Name);

            var LeaveTime = _TimeCard_BLL.Get().FirstOrDefault(a => a.UserId.Equals(UserId) && a.AddDate.Year == DateTime.Now.Year && a.AddDate.Month == DateTime.Now.Month && a.AddDate.Day == DateTime.Now.Day);
            if (LeaveTime != null)
            {
                LeaveTime.AfterWorkTime = DateTime.Now.TimeOfDay;
                _TimeCard_BLL.Save();
                return string.Format("{0}:{1}:{2}", LeaveTime.AfterWorkTime.Hours, LeaveTime.AfterWorkTime.Minutes, LeaveTime.AfterWorkTime.Seconds);
            }

            return "";

        }

        //修改主题
        public ActionResult Theme()
        {
            Sys_Theme_BLL _Sys_Theme_BLL = new Sys_Theme_BLL();
            var model = _Sys_Theme_BLL.Get();
            return View(model);
        }

        public void ChangeTheme(Guid Id)
        {

            _Sys_Users_BLL.ChangeTheme(Id, User.Identity.Name);
        }


        //系统帮助
        public ActionResult Help()
        {
            Sys_Controllers_BLL _Sys_Controllers_BLL = new Sys_Controllers_BLL();

            var model = _Sys_Controllers_BLL.Get().ToList();

            return View(model.ToList());
        }

        //修改密码
        public ActionResult Password()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Password(ChangePasswordModel item)
        {
            //  检测原密码 是否正确
            if (!_Sys_Users_BLL.check(new Guid(User.Identity.Name), item.OldPassword))
            {
                ModelState.AddModelError("OldPassword", "原密码错误！");
            }

            if (!ModelState.IsValid)
            {
                return View(item);
            }

            _Sys_Users_BLL.ChangePassword(new Guid(User.Identity.Name), item.NewPassword);

            ModelState.AddModelError("", "密码修改成功！");

            return View();
        }

        //用户消息中心 刷新 
        public ActionResult NewMsg()
        {
            //获取当前用户
            var user = _Sys_Users_BLL.Get(User.Identity.Name);

            //连接到查看？
            var model = _Sys_User_Notification_BLL.Get().Where(a => a.New && a.UserId.Equals(new Guid(User.Identity.Name)));
            if (model != null)
            {
                return View(model);
            }

            return null;
        }

        //用户消息中心 
        public ActionResult Notification(int? pageIndex, string keyword)
        {
            var model = _Sys_User_Notification_BLL.Get().Where(a => a.UserId.Equals(new Guid(User.Identity.Name)));
            if (!string.IsNullOrEmpty(keyword))
            {
                model = model.Where(a => a.Title.Contains(keyword));
            }
            return View(model.ToPagedList(pageIndex));
        }

        public ActionResult NotificationDetails(Guid Id)
        {
            var item = _Sys_User_Notification_BLL.Get(Id);
            item.New = false;
            _Sys_User_Notification_BLL.Save(Id, ref item);
            return RedirectToAction(item.Action, item.Controller, new { Id = item.RecordId });
        }

    }
}
